﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PstBakup2013Server
{
    public class TestSqlHelper
    {
        private static SqlHelper _sqlHelper = SqlHelper.GetInstance();
        private static string[] userNames = new string[] { "Courtel", "Dupont", "Grandjean", "Sarkozy", "Lepetit", "Legrand", "Lemoyen", "Lebreton", "Triffault", "Castagnier", "Mauco", "Platon", "Perche", "Dujardin", "Lemaçon" };
        private static Random rnd = new Random(DateTime.Now.Millisecond);
        private static System.Threading.Thread _threadServer;
        private static Server _server;

        public static int Main()
        {
            //if (ConnectToSqlServer())
            //{
            //    DumpResultLabelTable();

            //    for (int i = 0; i < 10; i++)
            //    {
            //        RegisterClient();
            //    }
            //    List<String> clientIDs = sqlHelper.GetClientIDs();
            //    foreach (String clientID in clientIDs)
            //    {
            //        sqlHelper.ExecuteCommand("UPDATE [PstBackup2013].[dbo].[tbClients] SET LastContactDate = '" + GetLastContactDate() + "' WHERE ClientID = '" + clientID + "'");
            //    }
            //}

            //_sqlHelper.DeleteAllPstFiles();
            //_sqlHelper.DeleteAllClients();

            Server.Settings serverSettings = new Server.Settings();
            serverSettings.ClientsPort = 42000;
            serverSettings.ReportsPort = 43000;
            _server = new Server(serverSettings);

            _threadServer = new System.Threading.Thread(new System.Threading.ThreadStart(_server.Start));
            _threadServer.IsBackground = false;
            _threadServer.Start();
                        
            int key = Console.Read();

            return 0;
        }

        private static bool ConnectToSqlServer()
        {
            _sqlHelper.ServerName = "SqlPstBackup";
            _sqlHelper.DataBaseName = "PstBackup2013";

            if (_sqlHelper.Connect(@"sa", "@dmlocal128"))
            {
                Console.WriteLine("Connected to database");
                return true;
            }
            else
                Console.WriteLine("Unable to connect to database");

            return false;
        }

        //private static void DumpResultLabelTable()
        //{
        //    _sqlHelper.GetLabelTable("SELECT [ResultID], [Label] FROM [dbo].[tbResultLabel]");
        //}

        private static void RegisterClient()
        {
            _sqlHelper.ExecuteCommand("INSERT INTO [PstBackup2013].[dbo].[tbClients] VALUES (" + GetClientValues() + ")");
        }

        private static string GetClientValues()
        {
            string values = string.Format("'{0}', '{1}', '{2}', '{3}'", Guid.NewGuid().ToString(), GetComputerName(), GetUserName(), DateTime.Now.ToString());

            return values;
        }

        private static string GetComputerName()
        {
            Random rnd = new Random(DateTime.Now.Millisecond);

            return "AD" + string.Format("{0:000}", rnd.Next(100, 899));
        }

        private static string GetUserName()
        {
            return userNames[rnd.Next(0, userNames.Length)];
        }

        private static DateTime GetLastContactDate()
        {
            return new DateTime(rnd.Next(2007, 2015), rnd.Next(1, 12), rnd.Next(1, 29), rnd.Next(0, 23), rnd.Next(0, 59), rnd.Next(0, 59));
        }

    }
}
